

//引入网络库
import { get, post } from "../../util/util.js"
// 获取全局应用程序实例对象
var app = getApp();

// 创建页面实例对象
Page({
  /**
   * 页面名称
   */ 
  name: "woderenzheng",
  /**
   * 页面的初始数据
   */

  data: {
    uid: '',
    selectedLeft: true,
    selectedRight: false,
    yaoqingStr: '',
    realNameJzStr: '',
    numberJzStr: '',
    zhengmianImg: '',
    beimianImg: '',

    sex: "",
    realNameStr: '',
    numberStr: '',
    nameStr: '',
    jiguanStr: '',
    changzhuStr: '',
    schoolStr: '',
    skillStr: '',
    //身高
    heightList: [],
    heightKey: '',
    //体型
    shapeList: [],
    shapeKey: '',
    //个人特征 
    skillSize: 1,
    skillKey: '',
    skillArr: [],
    //特长
    featureSize: 1,
    featureKey: '',
    featureArr: [],
    //语言
    languageSize: 1,
    languageKey: '',
    languageArr: [],
    //颜值
    faceSize: 1,
    faceKey: '',
    faceArr: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },

  onLoad: function (o) {
    var that = this;
    console.log("我的认证===" + o.uid);///tag/getConfig
    that.setData({
      uid: o.uid
    })
    post("/tag/getConfig", {}).then(data => {
      //身高
      var heightData = [];
      var heightStr = [];
      for (var key in data.data.height) {
        heightData.push(key);
        heightStr.push(data.data.height[key]);
      }
      // 体型
      var shapeData = [];
      var shapeStr = [];
      for (var key in data.data.shape) {
        shapeData.push(key);
        shapeStr.push(data.data.shape[key]);
      }
      //个人特征  
      var skillData = [];
      var skillStr = [];
      for (var key in data.data.skill) {
        for (var keyItem in data.data.skill[key]) {
          skillData.push(keyItem);
          skillStr.push(data.data.skill[key][keyItem]);
        }
      }
      var skillJson = [];
      for (var i = 0; i < skillStr.length; i++) {
        var j = {};
        j.val = skillData[i];
        j.name = skillStr[i];
        j.value = i;
        j.checked = false;
        skillJson.push(j);
      }

      //特长  
      var featureData = [];
      var featureStr = [];
      for (var key in data.data.feature) {
        featureData.push(key);
        featureStr.push(data.data.feature[key]);
      }
      var featureJson = [];
      for (var i = 0; i < featureStr.length; i++) {
        var j = {};
        j.val = featureData[i];
        j.name = featureStr[i];
        j.value = i;
        j.checked = false;
        featureJson.push(j);
      }
      //语言  
      var languageData = [];
      var languageStr = [];
      for (var key in data.data.language) {
        languageData.push(key);
        languageStr.push(data.data.language[key]);
      }
      var languageJson = [];
      for (var i = 0; i < languageStr.length; i++) {
        var j = {};
        j.val = languageData[i];
        j.name = languageStr[i];
        j.value = i;
        j.checked = false;
        languageJson.push(j);
      }
      //颜值  
      var faceData = [];
      var faceStr = [];
      for (var key in data.data.face) {
        faceData.push(key);
        faceStr.push(data.data.face[key]);
      }
      var faceJson = [];
      for (var i = 0; i < faceStr.length; i++) {
        var j = {};
        j.val = faceData[i];
        j.name = faceStr[i];
        j.value = i;
        j.checked = false;
        faceJson.push(j);
      }

      that.setData({
        heightList: heightData,
        heightArr: heightStr,

        shapeList: shapeData,
        shapeArr: shapeStr,

        skillArr: skillJson,

        featureArr: featureJson,

        languageArr: languageJson,

        faceArr: faceJson,
      })
    })
  },

  radioChange: function (e) {
    console.log("性别" + e.detail.value);
    var that = this;
    that.setData({
      sex: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  //邀请码
  yaoqingInput: function (e) {
    this.setData({
      yaoqingStr: e.detail.value
    })
  },

  //真实姓名 
  realNameJzInput: function (e) {
    this.setData({
      realNameJzStr: e.detail.value
    })
  },
  //身份证号   
  numberJzInput: function (e) {
    this.setData({
      numberJzStr: e.detail.value
    })
  },

  //真实姓名 
  realNameInput: function (e) {
    this.setData({
      realNameStr: e.detail.value
    })
  },
  //身份证号   
  numberInput: function (e) {
    this.setData({
      numberStr: e.detail.value
    })
  },
  //艺名 
  nameInput: function (e) {
    this.setData({
      nameStr: e.detail.value
    })
  },
  //籍贯  
  jiguanInput: function (e) {
    this.setData({
      jiguanStr: e.detail.value
    })
  },
  //常住地 
  changzhuInput: function (e) {
    this.setData({
      changzhuStr: e.detail.value
    })
  },
  //学校 
  schoolInput: function (e) {
    this.setData({
      schoolStr: e.detail.value
    })
  },
  //专业 
  skillInput: function (e) {
    this.setData({
      skillStr: e.detail.value
    })
  },

  /**
   * 获取点击 身高 字段
   */
  getSg: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);

    that.setData({
      currentItemSg: index,
      heightKey: that.data.heightList[index],
    })
  },
  /**
    * 获取点击 体型 字段
    */
  getTx: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log("体型选择" + that.data.shapeList[index]);
    that.setData({
      currentItemTx: index,
      shapeKey: that.data.shapeList[index],
    })
  },
  /**
  * 获取点击 特征 字段
  */
  getTz: function (e) {
    var that = this;
    var skillShenGao = that.data.skillArr;
    var size = that.data.skillSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.skillArr[index].val + "特征选择" + that.data.skillArr[index].checked);
    if (skillShenGao[index].checked) {
      size--;
      skillShenGao[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        skillShenGao[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      skillArr: skillShenGao,
      skillSize: size,
    })
  },

  /**
* 获取点击 特长 字段
*/
  getTc: function (e) {
    var that = this;
    var featureShenGao = that.data.featureArr;
    var size = that.data.featureSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.featureArr[index].val + "特长选择" + that.data.featureArr[index].checked);
    if (featureShenGao[index].checked) {
      size--;
      featureShenGao[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        featureShenGao[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      featureArr: featureShenGao,
      featureSize: size,
    })
  },
  /**
* 获取点击 语言 字段
*/
  getYy: function (e) {
    var that = this;
    var languageShenGao = that.data.languageArr;
    var size = that.data.languageSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.languageArr[index].val + "语言选择" + that.data.languageArr[index].checked);
    if (languageShenGao[index].checked) {
      size--;
      languageShenGao[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        languageShenGao[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      languageArr: languageShenGao,
      languageSize: size,
    })
  },

  /**
* 获取点击 颜值 字段
*/
  getYz: function (e) {
    var that = this;
    var faceShenGao = that.data.faceArr;
    var size = that.data.faceSize;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.faceArr[index].val + "颜值选择" + that.data.faceArr[index].checked);
    if (faceShenGao[index].checked) {
      size--;
      faceShenGao[index].checked = false;
    } else {
      if (size <= 5) {
        size++;
        faceShenGao[index].checked = true;
      } else {
        wx.showModal({
          title: '提示',
          content: '亲~标签不能超过5个哟',
          showCancel: false, //不显示取消按钮
          confirmText: '确定'
        })
      }
    }
    that.setData({
      faceArr: faceShenGao,
      faceSize: size,
    })
  },

/**
 * formId获取
 */
  submitInfo: function (e) {
    var that = this;
    console.log('表单submitInfo:==================', e);
    console.log('表单id:==================', e.detail.formId);
   
    post("/user/addFormid", { uid: that.data.uid, form_id: e.detail.formId}).then(data => {
    })
  },
  /**
   * 剧组认证
   */
  juzuSub: function () {
    var that = this;
    if (that.data.realNameJzStr != "" && that.data.numberJzStr != ""
      && that.data.zhengmianImg != "" && that.data.beimianImg != "") {
      //查看是否认证
      post("/user/getAudit", { uid: that.data.uid }).then(data => {
        console.log('查看是否认证====================' + data.data);
        if (data.data.status == -1) {
          wx.showModal({
            title: '提示',
            content: '您提交的资料正在审核中',
            showCancel: false,
            confirmText: '确定'
          })
        } else
          if (data.data.status == 1) {
            wx.showModal({
              title: '提示',
              content: '您的剧组认证已通过审核',
              showCancel: false,
              confirmText: '确定'
            })
          } else {//data.data.status == 0 || data.data.status == 2
            //post请求数据 提交认证 
            post("/user/submitAudit", {
              uid: that.data.uid,
              role: 4,
              realname: that.data.realNameJzStr,
              idno: that.data.numberJzStr,
              // in_param: sfz,
              idpic_f: that.data.zhengmianImg,
              idpic_b: that.data.beimianImg,
            }).then(data => {
              console.log('提交认证====================' + data.data);
              if (data.data.code == 0) {
                wx.showModal({
                  title: '提示',
                  content: '剧组认证信息已成功提交',
                  showCancel: false, //不显示取消按钮
                  confirmText: '确定'
                })
              }
            })
          }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请将资料填写完整',
        showCancel: false, //不显示取消按钮
        confirmText: '确定'
      })
    }

  },

  getZm: function (e) {
    console.log('getZm======getZm==========getZm====');
    var that = this;

    wx.chooseImage({
      count: 1, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        console.log('图片路径' + tempFilePaths[0]);
        that.setData({
          zhengmianImg: tempFilePaths,
        })
      }
    })
  },

  getBm: function (e) {
    console.log('getBm======getBm==========getBm====');
    var that = this;

    wx.chooseImage({
      count: 1, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        console.log('图片路径' + tempFilePaths[0]);
        that.setData({
          beimianImg: tempFilePaths,
        })
      }
    })
  },

  yirenSub: function () {
    var that = this;
    var yanzhi = [];
    for (var i = 0; i < that.data.faceArr.length; i++) {
      if (that.data.faceArr[i].checked) {
        yanzhi.push(parseInt(that.data.faceArr[i].val));
      }
    }
    var tezheng = [];
    for (var i = 0; i < that.data.skillArr.length; i++) {
      if (that.data.skillArr[i].checked) {
        tezheng.push(parseInt(that.data.skillArr[i].val));
      }
    }
    console.log("特征传递参数=======" + JSON.stringify(tezheng));
    var techang = [];
    for (var i = 0; i < that.data.featureArr.length; i++) {
      if (that.data.featureArr[i].checked) {
        techang.push(parseInt(that.data.featureArr[i].val));
      }
    }
    console.log("特长传递参数=======" + JSON.stringify(techang));
    var yuyan = [];
    for (var i = 0; i < that.data.languageArr.length; i++) {
      if (that.data.languageArr[i].checked) {
        yuyan.push(parseInt(that.data.languageArr[i].val));
      }
    }
    console.log("语言传递参数=======" + JSON.stringify(yuyan));

    if (that.data.realNameStr != "" && that.data.numberStr != ""
      && that.data.nameStr != "" && that.data.sex != ""
      && that.data.jiguanStr != "" && that.data.changzhuStr != ""
      && that.data.schoolStr != "" && that.data.skillStr != ""
      && that.data.shapeKey != "" && that.data.heightKey != ""
      && yanzhi.length > 0) {
      var param = {};
      param.uid = that.data.uid;
      param.role = 2;
      param.realname = that.data.realNameStr;//艺名 nameStr 性别 sex
      param.idno = that.data.numberStr;
      param.stage_name = that.data.nameStr;
      param.sex = that.data.sex;
      param.native_place = that.data.jiguanStr;
      param.live_place = that.data.changzhuStr;
      param.grad_school = that.data.schoolStr;
      param.school_major = that.data.skillStr;
      param.shape_group = parseInt(that.data.shapeKey);
      param.height_group = parseInt(that.data.heightKey);
      param.face_group = yanzhi;
      param.feature_group = techang;
      param.skill_group = tezheng;
      param.language = yuyan;
      if (that.data.yaoqingStr != "") {
        param.invite_code = that.data.yaoqingStr;
      }
      //查看是否认证
      post("/user/getAudit", { uid: that.data.uid }).then(data => {
        console.log('查看是否认证====================' + data.data);
        if (data.data.status == -1) {
          wx.showModal({
            title: '提示',
            content: '您提交的资料正在审核中',
            showCancel: false,
            confirmText: '确定'
          })
        } else if (data.data.status == 1) {
            wx.showModal({
              title: '提示',
              content: '您的剧组认证已通过审核',
              showCancel: false,
              confirmText: '确定'
            })
          } else {//data.data.status == 0 || data.data.status == 2
            //post请求数据 提交认证
            post("/user/submitAudit", param).then(data => {
              console.log('提交认证====================' + data.data);
              if (data.code == 0) {
                wx.showModal({
                  title: '提示',
                  content: '剧组认证信息已成功提交',
                  showCancel: false, //不显示取消按钮
                  confirmText: '确定'
                })
              } else if (data.code == 4042) {
                wx.showModal({
                  title: '未购买该服务',
                  content: data.msg,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      post("/pay/getPayItem", { cate: 3 }).then(data => {
                        console.log("-getPayItem======" + JSON.stringify(data.data));
                        post("/pay/payItem", { item_id: data.data.id, uid: that.data.uid }).then(data1 => {
                          if (data1.code == 0) {
                            wx.showToast({
                              title: data1.msg,
                              icon: 'succes',
                              duration: 1000,
                              mask: true
                            })
                          } else if (data1.code == 4011) {
                            wx.navigateTo({
                              url: '../pay/pay?uid=' + that.data.uid,
                            })
                          }
                        })
                      })
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              }
            })
          }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请将资料填写完整',
        showCancel: false, //不显示取消按钮
        confirmText: '确定'
      })
    }

  },

  selectedLeft: function (e) {
    this.setData({
      selectedRight: false,
      selectedLeft: true
    })
  },
  selectedRight: function (e) {
    this.setData({
      selectedLeft: false,
      selectedRight: true
    })
  }
})

